import { useRouter, useRoute } from "vue-router";
import { PxMessage, PxMessageBox } from "px-ui";
import { delInspectTemplate } from "@/api/equip/inspectTemplate";
export const useAction = () => {
  const router = useRouter();
  const {
    query: { page },
    fullPath
  } = useRoute();
  const newFullPath = encodeURIComponent(fullPath);
  const view = row => {
    router.push(`/all/equip/inspect/template/detail?id=${row.id}&page=${newFullPath}`);
  };
  const add = () => {
    router.push(`/all/equip/inspect/template/add?page=${newFullPath}`);
  };
  const edit = row => {
    router.push(`/all/equip/inspect/template/edit?id=${row.id}&page=${newFullPath}`);
  };
  const copy = row => {
    router.push(`/all/equip/inspect/template/copy?id=${row.id}&page=${newFullPath}`);
  };
  const deleteTemplate = (row, callBack?: Function) => {
    PxMessageBox.confirm(`要删除巡检编号为“${row.temCode}”的巡检模板吗?删除后不可恢复，请谨慎操作`, "提示", {
      confirmButtonText: "删除",
      confirmButtonClass: "px-button--danger",
      cancelButtonText: "取消",
      type: "warning",
      draggable: true
    })
      .then(async () => {
        await delInspectTemplate(row.id);
        PxMessage.success("删除成功");
        callBack && callBack();
      })
      .catch(() => {});
  };
  const back = (newPath?: string) => {
    if (newPath) {
      router.push(newPath);
    } else if (page) {
      router.push(page as string);
    } else {
      router.go(-1);
    }
  };
  return {
    view,
    add,
    edit,
    copy,
    delete: deleteTemplate,
    back
  };
};
